Imports ComponentGo.Calendars

Namespace Helpers

    Friend Class BreakComparer : Implements IComparer
        Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim b1 As ComponentGo.Calendars.Break = x
            Dim b2 As ComponentGo.Calendars.Break = y
            Return TimeSpan.Compare(b1.BeginBreak, b2.BeginBreak)
        End Function

        Private Shared mInstance As BreakComparer
        Public Shared ReadOnly Property Instance() As BreakComparer
            Get
                If mInstance Is Nothing Then
                    mInstance = New BreakComparer
                End If
                Return mInstance
            End Get
        End Property
    End Class

    Public Class BreakManager
        Private mModified As Boolean = False
        Private mExtra As Boolean = False
        Private mBreaks As ComponentGo.Calendars.BreakCollection = New BreakCollection

        Public ReadOnly Property Breaks() As BreakCollection
            Get
                Return mBreaks
            End Get
        End Property


        Public Property Modified() As Boolean
            Get
                Return mModified
            End Get
            Set(ByVal Value As Boolean)
                mModified = Value
            End Set
        End Property

        Public ReadOnly Property Extra() As Boolean
            Get
                Return mExtra
            End Get
        End Property

        Public Sub New()
            MyClass.New(False)
        End Sub

        Public Sub New(ByVal extra As Boolean)
            mExtra = extra

            mBreaks.Clear()
            If Not mExtra Then
                mBreaks.Add(New Break(TimeSpan.Zero, New TimeSpan(24, 0, 0), extra))
            End If
        End Sub

        Public Sub New(ByVal bm As BreakManager)
            mExtra = bm.Extra

            For Each b As Break In bm.Breaks
                mBreaks.Add(New Break(b.BeginBreak, b.EndBreak, b.Extra))
            Next
        End Sub


        Public Sub SetHoliday()
            mBreaks.Clear()
            mBreaks.Add(New Break(TimeSpan.Zero, New TimeSpan(24, 0, 0), Extra))
        End Sub


        Public Sub AddBreak(ByVal BeginTime As TimeSpan, ByVal EndTime As TimeSpan)
            Dim NewBreaks As New ComponentGo.Calendars.BreakCollection
            Dim i As Integer = 0

            For Each b As Break In Breaks
                If TimeSpan.Compare(BeginTime, b.BeginBreak) < 0 Then
                    If TimeSpan.Compare(EndTime, b.BeginBreak) < 0 Then
                        NewBreaks.Add(b)
                    ElseIf TimeSpan.Compare(EndTime, b.EndBreak) <= 0 Then
                        NewBreaks.Add(New Break(BeginTime, b.EndBreak, Extra))
                        BeginTime = TimeSpan.Zero
                        EndTime = TimeSpan.Zero
                    End If
                ElseIf TimeSpan.Compare(BeginTime, b.EndBreak) <= 0 Then
                    If TimeSpan.Compare(EndTime, b.EndBreak) < 0 Then
                        NewBreaks.Add(b)
                        BeginTime = TimeSpan.Zero
                        EndTime = TimeSpan.Zero
                    Else
                        NewBreaks.Add(New Break(b.BeginBreak, EndTime, Extra))
                        BeginTime = TimeSpan.Zero
                        EndTime = TimeSpan.Zero
                    End If
                Else
                    NewBreaks.Add(b)
                End If
            Next

            If TimeSpan.Compare(EndTime, TimeSpan.Zero) <> 0 Then
                NewBreaks.Add(New Break(BeginTime, EndTime, Extra))
            End If

            NewBreaks.Sort(BreakComparer.Instance)
            mBreaks = NewBreaks
            Modified = True
        End Sub



        Public Sub RemoveBreak(ByVal BeginTime As TimeSpan, ByVal EndTime As TimeSpan)
            Dim NewBreaks As New ComponentGo.Calendars.BreakCollection
            Dim i As Integer = 0

            For Each b As Break In Breaks
                If TimeSpan.Compare(BeginTime, b.BeginBreak) < 0 Then
                    If TimeSpan.Compare(EndTime, b.BeginBreak) <= 0 Then
                        NewBreaks.Add(b)
                    ElseIf TimeSpan.Compare(EndTime, b.EndBreak) < 0 Then
                        NewBreaks.Add(New Break(EndTime, b.EndBreak, Extra))
                    End If
                ElseIf TimeSpan.Compare(BeginTime, b.EndBreak) < 0 Then
                    If TimeSpan.Compare(EndTime, b.EndBreak) < 0 Then
                        NewBreaks.Add(New Break(b.BeginBreak, BeginTime, Extra))
                        NewBreaks.Add(New Break(EndTime, b.EndBreak, Extra))
                    Else
                        NewBreaks.Add(New Break(b.BeginBreak, BeginTime, Extra))
                    End If
                Else
                    NewBreaks.Add(b)
                End If
            Next

            NewBreaks.Sort(BreakComparer.Instance)
            mBreaks = NewBreaks
            Modified = True
        End Sub


        Public Sub AddWorkTime(ByVal BeginTime As TimeSpan, ByVal EndTime As TimeSpan)
            Dim NewBreaks As New ComponentGo.Calendars.BreakCollection
            Dim i As Integer = 0

            For Each b As ComponentGo.Calendars.Break In Breaks
                If TimeSpan.Compare(BeginTime, b.BeginBreak) < 0 Then
                    If TimeSpan.Compare(EndTime, b.BeginBreak) <= 0 Then
                        NewBreaks.Add(b)
                    ElseIf TimeSpan.Compare(EndTime, b.EndBreak) < 0 Then
                        NewBreaks.Add(New Break(EndTime, b.EndBreak, Extra))
                    End If
                ElseIf TimeSpan.Compare(BeginTime, b.EndBreak) < 0 Then
                    If TimeSpan.Compare(EndTime, b.EndBreak) < 0 Then
                        NewBreaks.Add(New Break(b.BeginBreak, BeginTime, Extra))
                        NewBreaks.Add(New Break(EndTime, b.EndBreak, Extra))
                    Else
                        NewBreaks.Add(New Break(b.BeginBreak, BeginTime, Extra))
                    End If
                Else
                    NewBreaks.Add(b)
                End If
            Next

            NewBreaks.Sort(BreakComparer.Instance)
            mBreaks = NewBreaks
            Modified = True
        End Sub
    End Class

End Namespace
